﻿Imports System.IO
Imports System.Drawing.Imaging
Imports System.Data.SqlClient
Imports HtmlRichText
Imports innerLibs


Public Class TELA_EDITARHACK
    Public Banco As New InnerLibs.Databases.SQL(My.Settings.Conexao)

    Dim HACK_ID As Integer = 0
    Private Sub TELA_EDITARHACK_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        HACK_AUTOR.Text = My.Settings.NomeUsuario
        Dim cats As SqlDataReader = Banco.ExecuteSQL("SELECT * From HackCategoria where CAT_ATIVO = 1")
        While cats.Read
            HACK_CATEGORIA.Items.Add(cats("CAT_ID") & " | " & cats("CAT_NOME"))
        End While
    End Sub


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim pegaimagem As New OpenFileDialog
        pegaimagem.Multiselect = False
        pegaimagem.Title = "Selecionar Captura de Tela"
        pegaimagem.Filter = "Imagens |*.JPG; *.JPEG; *.PNG; *.BMP"
        If pegaimagem.ShowDialog() = Windows.Forms.DialogResult.OK Then
            HACK_SCREEN.Image = Image.FromFile(pegaimagem.FileName)
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim moedinha = MostrarMoeda()
        Try
            Dim fStream As New FileStream(NomeRom.Text, FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(fStream)
            Dim HACK_ARQUIVO = br.ReadBytes(CInt(New FileInfo(NomeRom.Text).Length))
            br.Close()
            fStream.Close()
            Dim con As SqlConnection = New SqlConnection(My.Settings.Conexao)
            Dim com As SqlCommand = New SqlCommand
            com.Connection = con
            'set parameters
            Dim p1 As SqlParameter = New SqlParameter("@CAT_ID", SqlDbType.VarChar)
            Dim CAT_ID() = HACK_CATEGORIA.Text.Split("|")
            p1.Value = CAT_ID(0)
            Dim p2 As SqlParameter = New SqlParameter("@USU_ID", SqlDbType.Int)
            p2.Value = My.Settings.IdUsuario
            Dim p3 As SqlParameter = New SqlParameter("@HACK_NOME", SqlDbType.VarChar)
            p3.Value = HACK_NOME.Text
            Dim p4 As SqlParameter = New SqlParameter("@HACK_DESCRICAO", SqlDbType.VarChar)
            p4.Value = HACK_DESCRICAO.Text
            Dim p5 As SqlParameter = New SqlParameter("@HACK_IMAGEM", SqlDbType.VarChar)
            p5.Value = HACK_SCREEN.BackgroundImage.toBase64(ImageFormat.Png)
            Dim p6 As SqlParameter = New SqlParameter("@HACK_ARQUIVO", SqlDbType.VarBinary)
            p6.Value = HACK_ARQUIVO
            Dim p7 As SqlParameter = New SqlParameter("@HACK_VERSAO", SqlDbType.VarChar)
            p7.Value = HACK_VERSAO.Text
            Dim p8 As SqlParameter = New SqlParameter("@HACK_ID", SqlDbType.Int)
            p8.Value = HACK_ID

            com.Parameters.Add(p1)
            com.Parameters.Add(p2)
            com.Parameters.Add(p3)
            com.Parameters.Add(p4)
            com.Parameters.Add(p5)
            com.Parameters.Add(p6)
            com.Parameters.Add(p7)
            com.Parameters.Add(p8)

            com.CommandText = "EXEC piuUploadHack @HACK_ID, @CAT_ID, @USU_ID, @HACK_NOME, @HACK_DESCRICAO, @HACK_IMAGEM, @HACK_ARQUIVO, @HACK_VERSAO"
            con.Open()
            'insert the file into database
            com.ExecuteNonQuery()
            con.Close()

            MsgBox("Upload de HACK feito com sucesso!")
            HACK_ID = 0
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Controls.Remove(moedinha)
    End Sub



    


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles NomeRom.Click
        Dim pegahack As New OpenFileDialog
        pegahack.Multiselect = False
        pegahack.Title = "Selecionar ROM"
        pegahack.Filter = "Hacks de Super Mario World |*.smc"
        If pegahack.ShowDialog() = Windows.Forms.DialogResult.OK Then
            sender.text = pegahack.FileName
        End If
    End Sub

    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles desc.Click
        Dim pegatexto As New OpenFileDialog
        pegatexto.Multiselect = True
        pegatexto.Title = "Selecionar Arquivo de Texto"
        pegatexto.Filter = "Arquivos de Texto |*.txt; *.htm; *.html"
        If pegatexto.ShowDialog() = Windows.Forms.DialogResult.OK Then
            For Each arquivo In pegatexto.FileNames
                HACK_DESCRICAO.AddHTML(File.OpenText(arquivo).ReadToEnd() & vbNewLine & vbNewLine)
            Next
        End If

    End Sub

    Private Sub HACK_NOME_TextChanged(sender As Object, e As EventArgs) Handles HACK_NOME.TextChanged, HACK_AUTOR.TextChanged
        Me.Text = "Publicar Nova Hack - " & HACK_NOME.Text & " por " & HACK_AUTOR.Text & " Versão: " & HACK_VERSAO.Text
    End Sub
End Class